package com.k4rthik.clipall.ui;

import com.k4rthik.clipall.commons.Logger;
import com.k4rthik.clipall.network.ShareClient;
import com.k4rthik.clipall.network.profile.Profile;
import com.k4rthik.clipall.network.profile.ProfileManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA.
 * User: Perseus
 * Date: 7/4/12
 * Time: 6:39 PM
 */

public class NewConnectionsTab extends JPanel
{
    public NewConnectionsTab()
    {
        super(new FlowLayout(FlowLayout.LEFT));

        //Set Server to connect to
        JLabel serverLabel = new JLabel("Server IP: ");
        final JTextField serverIP = new JTextField();
        serverIP.setColumns(15);

        //Profile Selector
        JLabel profileLabel = new JLabel("Profile: ");
        JComboBox profileSelector = new JComboBox();

        addDummyProfiles();

        java.util.List<Profile> profileList = ProfileManager.getInstance().getRegisteredProfiles();
        for(int i=0; i<profileList.size(); i++)
        {
            profileSelector.addItem(profileList.get(i).getProfileName());
        }

        add(serverLabel);
        add(serverIP);

        add(profileLabel);
        add(profileSelector);

        JButton connectButton = new JButton("Connect");
        connectButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String serverName = serverIP.getText();
                if(!serverName.trim().equals(""))
                {
                    new ShareClient(serverName).connectToServer();
                    Logger.log("Connected to server:"+serverName);
                }
            }
        });
        add(connectButton);
        add(new JLabel("Connect to a remote machine using an IP and a profile."));
    }

    private void addDummyProfiles()
    {
        for(int i=0; i<5; i++) {
            ProfileManager.getInstance().createAndAddNewProfile();
        }
    }
}
